//查询事件和角色id
var eventids;
var events = {
	getEventsList : function() {
		var ecdClassRoomName = sessionStorage.getItem("ecdClassRoomName");
		$("#ecdClassRoomName").html(ecdClassRoomName);
		var name = sessionStorage.getItem("name");
		$("#login_name1").html(name);
		var crid = sessionStorage.getItem("crid");
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getevents,crid="
							+ crid
							+ "&address=http://103.37.166.4:7777/StateGrid/cra.do",
					scriptCharset : 'utf-8',
					success : function(param) {
						// 将json字符串转换成json对象
						sessionStorage.setItem("param", param);
						param = JSON.parse(param).data;

						events.getRolemsgs();
					},
					error : function() {
					}

				});
	},
	getRolemsgs : function() {
		var param = JSON.parse(sessionStorage.getItem("param")).data;
		for ( var i in param) {
			if (param[i].alive == "1") {
				eventids = param[i].ceventId
			}
		}
		var eventid = eventids;
		sessionStorage.setItem("eventid", eventid);

		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid="
							+ eventid
							+ "&address=http://103.37.166.4:7777/StateGrid/ua.do",
					scriptCharset : 'utf-8',
					success : function(param) {
//						alert(param);
						param = JSON.parse(param);
						users.createUserList(param.data,"#userList");
						// alert(param);
						events.enenTusers(param.data);
					},
					error : function() {
					}

				});
	},
	enenTusers : function(param) {
		var suid = sessionStorage.getItem("suid_name");
		// var param = JSON.parse(sessionStorage.getItem("param")).data;
		var recvid = "";
		var ecdrepName = ""
		for ( var i in param) {
			// var recvids=param[i].ecdrepId;
			for ( var j in param[i].realizeUsers) {
				if (param[i].realizeUsers[j].su_id == suid) {
					recvid = param[i].ecdrepId;
					sessionStorage.setItem("recvid", recvid);
					ecdrepName = param[i].ecdrepName

				}
			}
		}
		$("#login_name2").html(ecdrepName);
		if (recvid != "") {
			var eventid = sessionStorage.getItem("eventid");
			$.ajax({
						type : "GET",
						url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getrolemsgs,eventid="
								+ eventid
								+ ",recvid="
								+ recvid
								+ "&address=http://103.37.166.4:7777/StateGrid/crma.do",
						scriptCharset : 'utf-8',
						success : function(param) {
//							alert(param);
							param = JSON.parse(param);
							var messagecount = param.messagecount;
							events.createMessageTable(param.data);
							events.getUsers(eventid)
							$("#xinxin").html(messagecount);
						},
						error : function() {
						}

					});
		}

	},
	createMessageTable : function(messages) {
		$("#newTan").html("");
		var j = 0;
		var n = 0;
		var str = "";
		for ( var i in messages) {
			var isRead;
			isRead="<button>未读</button>";
			if (messages[i].ecdEventMessageIsReaded == "0") {
				n++;
				isRead="<button>未读</button>";

			} else if (messages[i].ecdEventMessageIsReaded == "1") {
				isRead="<button>已读</button>";
			}
			j++;
			str += '<div class="newDiv"><div  class="newTop" onclick="readmsg.consoleText(\''
					+ messages[i].ecdEventMessageId
					+ '\',\''
					+ messages[i].ecdEventMessageIsReaded
					+ '\')"><p>'
					+ j
					+ '、'
					+ messages[i].messageReceiverName
					+ ''
					+ messages[i].ecdEventMessageSendTime
					+ '</p>'+ isRead +'</div><div class="newCenter" onclick="readmsg.consoleText(\''
					+ messages[i].ecdEventMessageId + '\',\''
					+ messages[i].ecdEventMessageIsReaded + '\')"><p>'
					+ messages[i].ecdEventMessageContent + '</p></div></div>';
		}
		$(str).appendTo("#newTan");
		$("#weidu").html(n);
	},
	// 获取用户
	getUsers : function(eventid) {
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid="
							+ eventid
							+ "&address=http://103.37.166.4:7777/StateGrid/ua.do",
					scriptCharset : 'utf-8',
					success : function(param) {
						param = JSON.parse(param).data;
						events.createUserList(param);
					},
					error : function() {
					}

				});
	},
	createUserList : function(users) {

		var str = "";
		for (var i = 0; i < users.length; i++) {
			if (users[i].realizeUsers.length > 0) {
				str += '<li class="current"><span>' + users[i].ecdrepName
						+ '</span><ul>';
				for (var j = 0; j < users[i].realizeUsers.length; j++) {
					str += '<li><li><a href="javascript:;">'
							+ users[i].realizeUsers[j].su_name
							+ '</a></li></ul></li>'
				}
				str += '</ul></li>';
			}
		}
		$(str).appendTo("#userList_c");
	},

}

var readmsg = {
	// 获取循环Id
	consoleText : function(id, stats) {
		console.info(id);// js控制台输出
		console.info(stats)
		if (stats == "0") {
			stats = "已读";
		} else if (stats == "1") {
			stats = "已读";
		}

		readmsg.readmsg_id(id);
	},
	readmsg_id : function(id) {
		var msgid = id;
		// sessionStorage.setItem("msgid", msgid);
		readmsg_appeal.currentMsgId = msgid;
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=readmsg,msgid="
							+ msgid
							+ "&address=http://103.37.166.4:7777/StateGrid/crma.do",
					scriptCharset : 'utf-8',
					success : function(param) {
//						alert(param)
						param = JSON.parse(param);
						readmsg.readmsg_list(param);
					},
					error : function() {
					}

				});
	},
	readmsg_list : function(param) {
		$("#sendOrder").html(param.data.sendOrder)// 消息编号
		sessionStorage.setItem("actime", param.data.ecdEventMessageSendTime)
		$("#ecdEventMessageSendTime").html(param.data.ecdEventMessageSendTime)// 时间
		$("#ecdEventMessageSender").html(param.data.ecdEventMessageSender)// 发送着
		$("#ecdEventMessageContent").html(param.data.ecdEventMessageContent)// 内容
		$("#xingdong_content").val(param.action.actionContents);
		var ownerId = param.data.ownerEventId
		var ownerEventId = JSON.parse(sessionStorage.getItem("param")).data;
		for ( var i in ownerEventId) {
			var readmsgId = ownerEventId[i].ceventId
			if (ownerId == readmsgId) {
				var ceventName = ownerEventId[i].ceventName
				$("#ownerEventId").html(ceventName)// 事件
			}
		}

		// 查询事件
	}
}
// 消息反馈
var actmsg = {
	actmsg_info : function() {
		var actext = $("#xingdong_content").val();// 行动内容
		var crid = sessionStorage.getItem("crid");// 演练室Id
		var eventid = sessionStorage.getItem("eventid");// 演练事件Id
		var msgid = readmsg_appeal.currentMsgId// 消息Id
		var postid = sessionStorage.getItem("recvid"); // 用户的演练角色id
		var suid = sessionStorage.getItem("suid_name"); // 用户身份Id
		var filename = $("#xiaoxi_accessory").val();// 行动的附件文件名称
		var date = sessionStorage.getItem("actime");
		var actime = "";// 行动的“演练虚拟”时间
//		date = new Date(date + time_num*1000);
		actime = new Date(date).Format("yyyyMMddHH:mm:ss");
//		current_date(date);
		// date = new Date(date);

//		var actime = "";// 行动的“演练虚拟”时间
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=actmsg,crid="
							+ crid
							+ ",eventid="
							+ eventid
							+ ",msgid="
							+ msgid
							+ ",postid="
							+ postid
							+ ",suid="
							+ suid
							+ ",actext="
							+ actext
							+ ",filename="
							+ filename
							+ ",actime="
							+ actime
							+ "&address=http://103.37.166.4:7777/StateGrid/crma.do",
					scriptCharset : 'utf-8',
					success : function(param) {
						alert(param)
//						param = JSON.parse(param).data;
//						readmsg.readmsg_list(param);
						$(".newPop").hide(500);
						$("body").removeClass("overHiden");
					},
					error : function() {
					}

				});
	},
	//回显
	actmsg_hx : function(param){
		$("#xingdong_content").html(params.actionContents);
		
	}
	
}

// 学员求助信息
var readmsg_appeal = {
	readmsg_student : function() {
		var msgid = readmsg_appeal.currentMsgId;//
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=readmsg,msgid="
							+ msgid
							+ "&address=http://103.37.166.4:7777/StateGrid/crma.do",
					scriptCharset : 'utf-8',
					success : function(param) {
//						alert(param)
						param = JSON.parse(param).data;
						readmsg.readmsg_list(param);
					},
					error : function() {
					}

				});
	},
	getroleques : function() {
		var crid = sessionStorage.getItem("crid");// 演练室Id
		var eventid = sessionStorage.getItem("eventid");// 演练事件Id
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getroleques,crid="
							+ crid
							+ ",eventid="
							+ eventid
							+ +"&address=http://103.37.166.4:7777/StateGrid/crma.do",
					scriptCharset : 'utf-8',
					success : function(param) {
//						alert(param)
						param = JSON.parse(param).data;
						readmsg.readmsg_list(param);
					},
					error : function() {
					}

				});
	},

	getHelp : function() {
		var msgid = readmsg_appeal.currentMsgId;//
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=helpmsg,msgid="
							+ msgid
							+ "&address=http://103.37.166.4:7777/StateGrid/crma.do",
					scriptCharset : 'utf-8',
					success : function(param) {
//						alert(param)
						// param = JSON.parse(param).data;
						// readmsg.readmsg_list(param);
					},
					error : function() {
					}

				});
	},
	currentMsgId : ""
}
// 查询学员的问答题列表
var getroleques = {
	getroleques_all : function() {
		var crid = sessionStorage.getItem("crid");// 演练室Id
		var eventid = sessionStorage.getItem("eventid");// 演练事件Id
		var suid = sessionStorage.getItem("suid_name"); // 用户身份Id
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getroleques,crid="
							+ crid
							+ ",eventid="
							+ eventid
							+ ",suid="
							+ suid
							+ "&address=http://103.37.166.4:7777/StateGrid/crqu.do",
					scriptCharset : 'utf-8',
					success : function(param) {
//						alert(param)
						param = JSON.parse(param).data;
						getroleques.getroleques_list(param);
					},
					error : function() {
					}

				});
	},
	getroleques_list : function(param) {
//		alert(param);
		$("#askDetails").html("");
		var str = "";
		var m = 0;
		var n = 0;
		for ( var i in param){
			var isAnswer="";
			isAnswer = "<button>未答</button>";
			if(param[i].questionLogAnswerContent ==" " || param[i].questionLogAnswerContent == null ){
				n++;
				isAnswer = "<button>未答</button>";
			}else if(param[i].questionLogAnswerContent != "" || param[i].questionLogAnswerContent != null ){
				isAnswer = "<button>已答</button>";
			}
			if(param[i].questionInfo == "" || param[i].questionInfo == null){//判断缺少的时候进行添加一个数组;
				param[i].questionInfo = {};
				param[i].questionInfo.questionText = "";
			}
			
			m++;
			str += '<div class="newDiv"><div class="newTop" onclick="getroleques.getroleques_Logid(\''
					+ param[i].questionLogId
					+ '\',\''
					+ param[i].answerPostName
					+ '\',\''
					+ param[i].ownerEventId
					+ '\',\''
					+ param[i].questionLogAskTime
					+ '\',\''
					+ param[i].questionLogAnswerTime
					+ '\',\''
					+ param[i].questionInfo.questionText
					+ '\',\''
					+ param[i].questionLogAnswerContent
					+ '\')"><p>'
					+ m
					+ '、'
					+ param[i].questionInfo.questionRange
					+ ' '
					+ param[i].questionLogAskTime
					+ '</p>'+isAnswer+'</div><div class="newCenter" onclick="getroleques.getroleques_Logid(\''
					+ param[i].questionLogId 
					+ '\',\''
					+ param[i].answerPostName
					+ '\',\''
					+ param[i].ownerEventId
					+ '\',\''
					+ param[i].questionLogAskTime
					+ '\',\''
					+ param[i].questionLogAnswerTime
					+ '\',\''
					+ param[i].questionInfo.questionText
					+ '\',\''
					+ param[i].questionLogAnswerContent
					+ '\')"><p>'
					+ param[i].questionInfo.questionText + '</p></div></div>';
			
		}
			
		$(str).appendTo("#askDetails");
		$("#tiaoshu").html(m);
		$("#weida").html(n)
	},
	//进行赋值
	getroleques_Logid : function(Logid,answerPostName,ownerEventId,questionLogAskTime,questionLogAnswerTime,questionText,questionLogAnswerContent) {
		console.info(Logid,answerPostName,ownerEventId,questionLogAskTime,questionLogAnswerTime,questionText,questionLogAnswerContent);// js控制台输出
		$("#answerPostName").html(answerPostName)//回答角色
		$("#questionLogAskTime").html(questionLogAskTime)//接收时间
		$("#questionLogAnswerTimes").html(questionLogAnswerTime)//回答时间
		$("#questionText").html(questionText)//提问内容
		$("#questionLogAnswerContent").val(questionLogAnswerContent)//回答内容
		sessionStorage.setItem("Logid", Logid)//日志Id
//		$("#ownerEventName").html(ownerEventId)//所属事件Id
		var ownerId = ownerEventId 
		var ownerEventId = JSON.parse(sessionStorage.getItem("param")).data; //说有事件
		for ( var i in ownerEventId) {
			var readmsgId = ownerEventId[i].ceventId
			if (ownerId == readmsgId) {
				var ceventName = ownerEventId[i].ceventName
				$("#ownerEventName").html(ceventName)// 事件
			}
		}
	},
	ansque : function(){
		var qlogid = sessionStorage.getItem("Logid");//问题日志的id
		var answer = $("#questionLogAnswerContent").val() //回答的内容
			answer   =   answer.replace(/\s+/g,""); 
		var	filename = ""//回答的文件附件(可选)
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=ansque,qlogid="+ qlogid + ",answer="+ answer + ",filename="+ filename + "&address=http://103.37.166.4:7777/StateGrid/crqu.do",
				scriptCharset : 'utf-8',
				success : function(param) {
//					alert(param)
					getroleques.getroleques_all();
					param = JSON.parse(param).data;
					alert(param.questionLogAnswerContent);
					$("#questionLogAnswerContent").val(param.questionLogAnswerContent)//回答内容
					$(".askDetailsPop").hide(500);
					$("body").removeClass("overHiden");
					
//					getroleques.getroleques_list(param);
				},
				error : function() {
				}

			});
	
		
	},
	//提问帮助
	gethelp : function(){
		var qlogid = sessionStorage.getItem("Logid");//问题日志的id
		$.ajax({
			type : "GET",
			url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=helpque,qlogid="
					+ qlogid
					+ "&address=http://103.37.166.4:7777/StateGrid/crqu.do",
			scriptCharset : 'utf-8',
			success : function(param){
//				alert(param)
//				param = JSON.parse(param).data;
//				getroleques.getroleques_list(param);
			},
			error : function() {
			}

		});
		
	}
}

//参考文档
var getmc = {
		getmc_wd : function(){
			$("#wnDang").html("");
			var crid = sessionStorage.getItem("crid");// 演练室Id
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getmc,crid="
						+ crid
						+ "&address=http://103.37.166.4:7777/StateGrid/mat.do",
				scriptCharset : 'utf-8',
				success : function(param){
//					alert(param)
					param = JSON.parse(param).data;
					getmc.getmc_list(param);
				},
				error : function() {
				}
			});
		},getmc_list : function(param){
			
			var str = "";
			str += '<tr><th>文档名称</th><th width="10%">操作</th></tr>';
			for(var i in param){
				
				str += '<tr><td>'+ param[i].materInfo.materFileName+'</a></td><td style="text-align: center;"><a href="http://103.37.166.4:7777/flexUpDownLoad03/upload/'+param[i].materInfo.materFileName+'"><span class="uploadO"></span></td></tr>';
			}
			$(str).appendTo("#wnDang");
			
								
			
			
			
		}
}



// 立即加载
$(function() {
	events.getEventsList();
	/*$("#eventFlow").click(function() {
		if($(this).hasClass('active')) {
			actmsg.actmsg_info();
		}
	});*/
	//回答信息
	$("#answer").click(function() {
		actmsg.actmsg_info();

	});
	//回答问题
	$("#awserWt").click(function() {
		getroleques.ansque();

	});
	//参考文档
	$("#newShow").click(function() {
		if($(this).hasClass('active')) {
			getmc.getmc_wd();
		} 
	});
	
	
	//消息求助
	$("#help").click(function() {
		readmsg_appeal.readmsg_student();
	});
	$("#iknow").click(function() {
		$('.dawawa').slideUp();
		setTimeout(function() {
			$('.dawawa').hide();
		});
	});
	// 局部刷新再调用方法
	$("#refresh").click(function() {
		events.getRolemsgs();
	});
	$("#refreshs").click(function() {
		getroleques.getroleques_all();
	});
	// 提问问题
	$("#ask").click(function() {
		if($(this).hasClass('active')) {
			getroleques.getroleques_all();
		} 
	});
	/*$("#ask").click(function() {
		getroleques.getroleques_all();

	})*/
})

/*
var users = {
		getUsers : function(id){
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid=" + eventids +"&address=http://103.37.166.4:7777/StateGrid/ua.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					param = JSON.parse(param).data;
					users.createUserList(param,id);
				},
				error: function(){
				}
				
			});
		},
		createUserList : function(users,id){
			$(id).html("");
			var str = "";
			if(id == "#userList_q"){
				for(var i=0;i<users.length;i++){
					if(users[i].realizeUsers.length >0){
						str += '<li><input type="checkbox" id="'+users[i].ecdrepId+'jsid" onclick="checkSingle(this)"><span  onclick="users.selectUser(this,\''+users[i].ecdrepId+'\',\''+id+'\')">'+users[i].ecdrepName+'</span><ul>';
						for(var j=0;j<users[i].realizeUsers.length;j++){
							str += '<li><input type="checkbox" id="'+users[i].realizeUsers[j].su_id+'yhid'+users[i].ecdrepId+'" onclick="checkSingle(this)"><a href="javascript:;">'+users[i].realizeUsers[j].su_name+'</a></li>';
						}
						str += '</ul></li>';
					}
				}
			}else{
				for(var i=0;i<users.length;i++){
					if(users[i].realizeUsers.length >0){
						str += '<li  onclick="users.selectUser(this,\''+users[i].ecdrepId+'\')"><span>'+users[i].ecdrepName+'</span><ul>';
						for(var j=0;j<users[i].realizeUsers.length;j++){
							str += '<li><a href="javascript:;">'+users[i].realizeUsers[j].su_name+'</a></li>';
						}
						str += '</ul></li>';
					}
				}
			}
			$(str).appendTo(id);
		},
		selectUser : function(u,ecdrepId,id){
			$(".current").css("background","");
			if(id == "#userList_q"){
				$(u).parent().toggleClass('current').siblings().removeClass('current');
			}else{
				$(u).toggleClass('current').siblings().removeClass('current');
			}
			if($(u).hasClass('current')){
				users.selectedUser = ecdrepId;
			}else{
				users.selectedUser = "";
			}
			$(".current").css("background","#fff9e8");
		},
		selectedUser : "",
		checkedUser : ""
}*/
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

